//
//  LogOutViewController.m
//  cc
//
//  Created by ZLQ on 2016/10/26.
//  Copyright © 2016年 hbyj. All rights reserved.
//

#import "CCLogOutViewController.h"
#import "Util.h"
#import "SDAssetsTableViewControllerCellModel.h"
#import "SDAssetsTableViewControllerCell.h"
#import "SVProgressHUD.h"
#import "KFBox.h"
@interface CCLogOutViewController ()

@end

@implementation CCLogOutViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.sectionsNumber = 3;
    self.cellClass = [SDAssetsTableViewControllerCell class];
    
    [self setupModel];
}

- (void)setupModel
{
    // section 0 的model
    SDAssetsTableViewControllerCellModel *model01 = [SDAssetsTableViewControllerCellModel modelWithTitle:@"账户管理" iconImageName:@"20000032Icon" destinationControllerClass:[SDBasicTableViewController class]];
    
    SDAssetsTableViewControllerCellModel *model02 = [SDAssetsTableViewControllerCellModel modelWithTitle:@"密码设置" iconImageName:@"20000059Icon" destinationControllerClass:[SDBasicTableViewController class]];
    
    SDAssetsTableViewControllerCellModel *model03 = [SDAssetsTableViewControllerCellModel modelWithTitle:@"安全设置" iconImageName:@"20000077Icon" destinationControllerClass:[SDBasicTableViewController class]];
    
    // section 1 的model
    SDAssetsTableViewControllerCellModel *model11 = [SDAssetsTableViewControllerCellModel modelWithTitle:@"我的客服" iconImageName:@"20000118Icon" destinationControllerClass:[SDBasicTableViewController class]];
    
    SDAssetsTableViewControllerCellModel *model12 = [SDAssetsTableViewControllerCellModel modelWithTitle:@"免责声明" iconImageName:@"20000180Icon" destinationControllerClass:[SDBasicTableViewController class]];
    
    SDAssetsTableViewControllerCellModel *model13 = [SDAssetsTableViewControllerCellModel modelWithTitle:@"关于" iconImageName:@"20000110Icon" destinationControllerClass:[SDBasicTableViewController class]];
    
    // section 2 的model
    SDAssetsTableViewControllerCellModel *model21 = [SDAssetsTableViewControllerCellModel modelWithTitle:@"退出登录" iconImageName:@"09999978Icon" destinationControllerClass:[SDBasicTableViewController class]];
    
    //model01,
    self.dataArray = @[@[model01, model02, model03],
                       @[model11, model12, model13],
                       @[model21]];
}

#pragma mark - delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section != 2) {
        SDAssetsTableViewControllerCellModel *model = [self.dataArray[indexPath.section] objectAtIndex:indexPath.row];
        UIViewController *vc = [[model.destinationControllerClass alloc] init];
        
        vc.title = model.title;
        [self.navigationController pushViewController:vc animated:YES];
    }else{
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"是否登出" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            //存储登录用户到单例
            UserEntity *userID = [Util getCurrentUserInfo];
            userID.isLogin = NO;
            userID.userName = @"";
            
            //删除沙盒下面的对应的 用户信息
            NSFileManager *manager = [[NSFileManager alloc] init];
            [manager removeItemAtPath:AccountFiledPath error:nil];
            
            [[NSNotificationCenter defaultCenter] postNotificationName:@"getInfo" object:nil userInfo:@{@"name":@"未登录",@"phone":@"未登录"}];
            
            [SVProgressHUD showSuccessWithStatus:@"登出成功"];
            
            //[KFBox setBoxAddTitle:@"温馨提示" addDtitle:@"是否登出" addController:self num:2];
            
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [SVProgressHUD dismiss];
                [self.navigationController popViewControllerAnimated:YES];
            });
        }];
        UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
            
        }];
        [alert addAction:action1];
        [alert addAction:action2];
        [self showDetailViewController:alert sender:nil];
        
        
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 20;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return (section == self.dataArray.count - 1) ? 10 : 0;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 60;
}


@end
